package com.shiwaixiangcun.monitor.controller;

import com.shiwaixiangcun.core.plugin.dto.MonkeyDto;
import com.shiwaixiangcun.core.plugin.web.validator.ConstraintType;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraint;
import com.shiwaixiangcun.core.plugin.web.validator.ParameterConstraints;
import com.shiwaixiangcun.core.repository.dictionary.DictionaryRepository;
import com.shiwaixiangcun.core.web.response.ResponseEntity;
import com.shiwaixiangcun.monitor.domain.SystemSetup;
import com.shiwaixiangcun.monitor.dto.*;
import com.shiwaixiangcun.monitor.service.EarlyWarningService;
import com.shiwaixiangcun.monitor.service.ElectronicFenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Created by Administrator on 2017-11-22.
 */
@Controller
public class EarlyWarningController {

    @Autowired
    private EarlyWarningService earlyWarningService;
    @Autowired
    private ElectronicFenceService electronicFenceService;
    @Autowired
    private DictionaryRepository dictionaryRepository;


    /**
     * 新增
     */
    @ParameterConstraints({
            @ParameterConstraint(name = "waringType", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "预警类型不能为空")),
            @ParameterConstraint(name = "identifier", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "标识不能为空")),
            @ParameterConstraint(name = "name", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "名字或者别名不能为空")),
            @ParameterConstraint(name = "status", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "状态不能为空"))
    })
    @RequestMapping(value = "/mi/earlyWarning/add", method = RequestMethod.POST)
    public String addEarlyWarning(MonkeyDto monkeyDto) {
        earlyWarningService.addEarlyWarning(monkeyDto);
        return "SUCCESS:添加成功";
    }

    /**
     * 处理预警
     */
    @RequestMapping(value = "/earlyWarning/handle", method = RequestMethod.POST)
    public String handleWaring(MonkeyDto monkeyDto) {
        earlyWarningService.handleWaring(monkeyDto);
        return "SUCCESS:处理成功";
    }

    /**
     * 预警历史列表
     */
    @RequestMapping(value = "/earlyWarning/list", method = RequestMethod.GET)
    public Page<WaringListDto> getWaringHistoryList(Pageable pageable, WaringSearchDto waringSearchDto) {
        return earlyWarningService.getWaringHistoryList(pageable, waringSearchDto);
    }

    /**
     * 紧急预警列表
     */
    @RequestMapping(value = "/dangerWarning/list", method = RequestMethod.GET)
    public List<DangerWarningListDto> getDangerWarningList() {
        return earlyWarningService.getDangerWarningList();
    }


    /*
    * 预警详情
    * **/
    @RequestMapping(value = "/dangerWarning/detail", method = RequestMethod.GET)
    public ResponseEntity getWarningDetail(Long waringId) {
        return new ResponseEntity().success(earlyWarningService.getWarningDetail(waringId));
    }

    /**
     * 全部信息列表
     */
    @RequestMapping(value = "/warning/all/list", method = RequestMethod.GET)
    public List<AllWaringListDto> getWarningAllList() {
        return earlyWarningService.getWarningAllList();
    }

    /**
     * 添加电子围栏点
     */
    @RequestMapping(value = "/mi/add/electronicFence", method = RequestMethod.POST)
    public String addElectronicFence(MonkeyDto monkeyDto) {
        electronicFenceService.addElectronicFence(monkeyDto);
        return "SUCCESS:添加成功";

    }

    /**
     * 获取电子围栏所有的点
     */
    @RequestMapping(value = "/electronicFence/list", method = RequestMethod.GET)
    public List<GeoLocationDto> getElectronicFence() {
        return electronicFenceService.getElectronicFence();
    }


    /**
     * 更新播放状态为TRUE
     */

    @ParameterConstraints({
            @ParameterConstraint(name = "waringId", constraintType = {ConstraintType.NotNull}, notNull = @NotNull(message = "预警id不能为空"))
    })
    @RequestMapping(value = "/update/play", method = RequestMethod.PUT)
    public String updatePlayStatus(Long waringId) {
        earlyWarningService.updatePlayStatus(waringId);
        return "SUCCESS:更新成功";
    }

    /**
     * 获取定位设置信息
     */
    @RequestMapping(value = "/get/LocationSet", method = RequestMethod.GET)
    public ResponseEntity getLocationSet() {
        return new ResponseEntity().success(earlyWarningService.getLocationSet());
    }

    /**
     * 更新状态
     */
    @RequestMapping(value = "/update/LocationSet", method = RequestMethod.PUT)
    public String updateLocationSet(Long id) {
        earlyWarningService.updateLocationSet(id);
        return "SUCCESS:更新成功";
    }

    /**
     * 查询主题
     * */
    @RequestMapping(value = "/get/theme", method = RequestMethod.GET)
    public ResponseEntity getThemeLists() {
        return new ResponseEntity().success(dictionaryRepository.findByType(SystemSetup.THEME_TYPE));
    }

    /**
     * 更新主题
     */
    @RequestMapping(value = "/update/theme", method = RequestMethod.PUT)
    public String updateTheme(Long id,Long themeId) {
        earlyWarningService.updateTheme(id,themeId);
        return "SUCCESS:更新成功";
    }


}
